var prefix = getRootPath()+"/${sname}";

$().ready(function() {
    validateRule();
});

function update() {
    $.ajax({
        cache : true,
        type : "POST",
        url : prefix+"/save",
        data : $('#edit-form').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code ==  "SUCCESS") {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(data.msg);
            }
        }
    });
}

function validateRule() {
    $.validator.setDefaults({
        ignore: ":hidden:not(.chosen-select)"
    });
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#edit-form").validate({
        rules : {
            <#list fields as f >
                <#if !f.pk>
                    <#if f_has_next>
            ${f.simpleName} : {
                required : true
            },
                    <#else>
            ${f.simpleName} : {
                required : true
            }
                    </#if>
                </#if>
            </#list>
        },
        messages : {
        <#list fields as f >
            <#if !f.pk>
                <#if f_has_next>
            ${f.simpleName} : {
                required : icon + "请输入${f.name}"
            },
            <#else>
            ${f.simpleName} : {
                required : icon + "请输入${f.name}"
            }
                </#if>
            </#if>
        </#list>
        },
        errorPlacement: function (error, element) {
            if (element.is("select")) {
                error.appendTo(element.parent());
            } else {
                error.insertAfter(element);
            }
        },
        submitHandler : function() {
            update();
        }
    })
}